package com.drawquest.gui.components;

import com.gooddies.graphics.ExtRectangle;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

/**
 * @author sad
 */
public class ImagePanel extends JPanel {

    private String url;
    private SoftReference<BufferedImage> imageReference = new SoftReference<>(null);
    private BufferedImage errorImage;
    private int horizontalGap = 0;
    private int verticalGap = 0;
    //final static Map<String, BufferedImage> urlToImageMap = Collections.synchronizedMap(new HashMap<String, BufferedImage>());

    public ImagePanel() {
        if (errorImage == null) {
            try {
                try (InputStream stream = this.getClass().getResourceAsStream("/com/drawquest/resources/error.png")) {
                    errorImage = ImageIO.read(stream);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //      urlToImageMap.put("https://drawquest.com/static/img/drawquest/avatar_default.png", errorImage);
        }

        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.GRAY));
    }

    public int getHorizontalGap() {
        return horizontalGap;
    }

    public void setHorizontalGap(int horizontalGap) {
        this.horizontalGap = horizontalGap;
    }

    public int getVerticalGap() {
        return verticalGap;
    }

    public void setVerticalGap(int verticalGap) {
        this.verticalGap = verticalGap;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        BufferedImage image = imageReference.get();
        if (image == null) {
            loadUrl(url);
            return;
        }

        ExtRectangle rect = new ExtRectangle(0, 0, image.getWidth(), image.getHeight());
        rect.fitProportional(new Rectangle(0, 0, getWidth() - horizontalGap * 3, getHeight() - verticalGap * 3));
        double dx = getWidth() / 2 - rect.getWidth() / 2;
        double dy = getHeight() / 2 - rect.getHeight() / 2;
        g.drawImage(image, (int) dx, (int) dy, (int) rect.getWidth(), (int) rect.getHeight(), this);
    }

    private void loadUrl(final String url) {
        SwingWorker ws = new SwingWorker() {
            BufferedImage loadedImage;

            @Override
            protected Object doInBackground() throws Exception {
                try {
                    if (url == null) {
                        loadedImage = errorImage;
                        return null;
                    }
                    if (url.equals("https://drawquest.com/static/img/drawquest/avatar_default.png")) {
                        loadedImage = errorImage;
                    } else {
                        InputStream stream = new URL(url).openStream();
                        loadedImage = ImageIO.read(stream);
                    }
                    return null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                return null;
            }

            @Override
            protected void done() {
                if (loadedImage == null) {
                    imageReference = new SoftReference<>(errorImage);
                } else {
                    imageReference = new SoftReference<>(loadedImage);
                }
                invalidate();
                repaint();
            }
        };
        ws.execute();
    }

    public void setUrl(final String url) {
        imageReference = new SoftReference<BufferedImage>(null);
        this.url = url;

    }
}
